package com.bamboocloud.ws.demo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bamboocloud.ws.demo.entity.AttributeEntity;
import com.banboocloud.Codec.BamboocloudFacade;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SchemaService extends HttpServlet {
	private static final long serialVersionUID = -943775192131572582L;
	//AES
	//	private static final String key = "123456";
	//	private static final String type = "AES";
	//	private static final String signType = "MD5";

	//SM4
	private static final String key = "JLB6L6MJVIT5Z7IL";
	private static final String type = "SM4";
	private static final String signType = "SM3";

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String bodyparam = BamboocloudUtils.getRequestBody(req);
		System.out.println("first--param-->" + bodyparam);
		//报文解密
		bodyparam = BamboocloudUtils.getPlaintext(bodyparam, key, type);
//		System.out.println("xml--param-->" + bodyparam);
//		bodyparam = XmlUtils.xmlToJson(bodyparam);
		System.out.println("json--param-->" + bodyparam);
		Map<String,Object> reqmap = (Map<String,Object>) JSON.parse(bodyparam);
		//验证签名
		if (BamboocloudUtils.verify(reqmap, signType).booleanValue()) {
			String username = (String) reqmap.get("bimRemoteUser");
			String password = (String) reqmap.get("bimRemotePwd");

			BamboocloudUtils.checkUsernamePassword(username, password);

			Map<String,Object> schema = new HashMap<String,Object>();
			List<AttributeEntity> attrList = new ArrayList<AttributeEntity>();

			AttributeEntity attr1 = new AttributeEntity();
			attr1.setType("String");
			attr1.setName("oaAccount");
			attr1.setRequired(true);
			attr1.setMultivalued(false);
			attrList.add(attr1);

			AttributeEntity attr2 = new AttributeEntity();
			attr2.setType("String");
			attr2.setName("orgId");
			attr2.setRequired(true);
			attr2.setMultivalued(false);
			attrList.add(attr2);

			AttributeEntity attr3 = new AttributeEntity();
			attr3.setType("String");
			attr3.setName("fullName");
			attr3.setRequired(true);
			attr3.setMultivalued(false);
			attrList.add(attr3);

			AttributeEntity attr4 = new AttributeEntity();
			attr4.setType("Boolean");
			attr4.setName("state");
			attr4.setRequired(true);
			attr4.setMultivalued(false);
			attrList.add(attr4);

			AttributeEntity attr5 = new AttributeEntity();
			attr5.setType("String");
			attr5.setName("password");
			attr5.setRequired(true);
			attr5.setMultivalued(false);
			attrList.add(attr5);

			List<AttributeEntity> orgattrlist = new ArrayList<AttributeEntity>();
			AttributeEntity org1 = new AttributeEntity();
			org1.setType("String");
			org1.setName("orgname");
			org1.setRequired(true);
			org1.setMultivalued(false);
			orgattrlist.add(org1);

			AttributeEntity org2 = new AttributeEntity();
			org2.setType("String");
			org2.setName("orgncode");
			org2.setRequired(true);
			org2.setMultivalued(false);
			orgattrlist.add(org2);

			AttributeEntity org3 = new AttributeEntity();
			org3.setType("String");
			org3.setName("parentcode");
			org3.setRequired(true);
			org3.setMultivalued(false);
			orgattrlist.add(org3);
			
			JSONObject jsonObject = JSONObject.parseObject(BamboocloudUtils.getProperty("schema"));
			List<AttributeEntity> orgs = jsonObject.getObject("organization", List.class);
			System.out.println(orgs.toString());
			List<AttributeEntity> accs = jsonObject.getObject("account", List.class);
			System.out.println(accs.toString());
			List<AttributeEntity> roles = jsonObject.getObject("role", List.class);
			System.out.println(roles.toString());
			schema.put("account", accs);
			schema.put("organization", orgs);
			schema.put("role", roles);
			schema.put("bimRequestId", reqmap.get("bimRequestId"));

			String mapJson = JSON.toJSONString(schema);
			System.out.println("response---json-->" + mapJson);
//			mapJson = XmlUtils.jsonToXml(mapJson);
//			System.out.println("response----->" + mapJson);
			mapJson = BamboocloudFacade.encrypt(mapJson, key, type);
			PrintWriter out = resp.getWriter();
			out.write(mapJson);
			out.close();
		}
	}

	public static void main(String[] args) {
		System.out.println(BamboocloudUtils.getProperty("schema"));
	}
}